#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <boost/filesystem/path.hpp>
#include <boost/program_options.hpp>
#include "get_arg.h"



// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp
//-lboost_filesystem-gcc-d
//-L/usr/local/lib





// my functions
#include <print_error.h>
#include <program_options_helper.h>
#include <stlhelper.h>
#define DEBUG_LEVEL 6
#include <debug_func.h>
#include <blast_results.h>
#include <codeml_results.h>

using std::string;
using std::cerr;
using std::string;
using std::ofstream;
using std::ifstream;
using std::cerr;
using std::cout;
using std::max;
using boost::filesystem::path;
#define DEFAULT_CODEML_PATH "/net/cpp-group/tools/paml/"
using namespace program_options_helper;
namespace po = boost::program_options_lg;

//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	//
	// checked mandatory arguments
	//
	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()
	("file1in,x", 			po::value<string>(&args.file_input1)->set_name("FILE"),
								"First file. The first two tab delimited columns contain "
								"sequence identifier pairs. ")
	("file2in,y", 			po::value<string>(&args.file_input2)->set_name("FILE"),
								 "Second file. The first two tab delimited columns "
								 "contain sequence identifier pairs. ");

	po::options_description hidden_args("Hidden Arguments");
	hidden_args.add_options()
	("debug", 					"Set all parameters for debug.\n");

	args.optional_args.add_options()
	("counts,c", 			po::value<string>(&args.file_summary)->set_name("FILE"),
								"Repeats information shown with --verbose.")
	("file1_only_ids,i",	po::value<string>(&args.file_1)
									->set_name("FILE"),
								"File to hold identifiers ONLY found in input_file1.")
	("file1_only_in_full,I",po::value<string>(&args.file_1_full)
									->set_name("FILE"),
								"File to hold results ONLY found in input_file1.")
	("file2_only_ids,j",	po::value<string>(&args.file_2)
									->set_name("FILE"),
								"File to hold identifiers ONLY found in input_file2.")
	("file2_only_in_full,J",po::value<string>(&args.file_2_full)
									->set_name("FILE"),
								"File to hold results ONLY found in input_file2.")
	("shared_ids,s",		po::value<string>(&args.file_shared)
									->set_name("FILE"),
								"File to hold identifiers found in BOTH input files.")
	("shared_in_full,S", 	po::value<string>(&args.file_shared_full)
									->set_name("FILE"),
								"File to hold results found in BOTH input files.")
	("merged_ids,m",		po::value<string>(&args.file_merged)
									->set_name("FILE"),
								"File to hold identifiers found in EITHER input files.")
	("merged_in_full,M", 	po::value<string>(&args.file_merged_full)
									->set_name("FILE"),
								 "File to hold results found in EITHER input files.")
	("#comment,#",			po::value<string>(&args.comment_letters)
									->set_name("LETTERS")
									->default_value("#"),
								"Use one of these letters to begin a "
								"commented line.");
	


	args.add_std_options();
	po::options_description all_args("");
	all_args.add(mandatory_args).add(args.optional_args).add(hidden_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.5\n"
			"\tHistory: \n\n"
			"\tv 1.0"
			"13-01-2005\1"
			"First Version\1"
			"Uses crc hashes. Which is why STDIN is not used.\n"
			"\tv 1.1"
			"05-06-2005\1"
			"Rename programme from seq_pairs_find_matching_results "
			"and simplified parameters\n"
			"\tv 1.2"
			"14-06-2005\1"
			"Tested and added debug mode.\n"
			"\tv 1.3\1"
			"04-01-2006\1"
			"For Andreas: Hash start comment lines. Fix bugs in handling input. "
			"Upgrade to common code for sequence pair identifiers\n"
			"\tv 1.4\1"
			"27-03-2006\1"
			"Comments can begin with user-defined letters. Defaults to hash '#'\n"
			"\tv 1.5\1"
			"29-03-2006\1"
			"Print destination file names. If file 2 is a subset of file1, "
			"it won't be printed at all.\n"
			"\tv 1.6\1"
			"29-05-2006\1"
			"Optimise for small diffs in large files: Saves file positions and "
			"jumps to these when writing out, without require a third line by line "
			"pass. Optimisation to avoid unnecesary slow fileseek: Checks filepos "
			"to see if successive lines being written.\n"
			);
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
					"Takes pairs of files and prints out matching lines or identifiers.\n"
					"Only the first instance of each duplicate will be selected.\n";

		po::options_description visible_args("");
		visible_args.add(mandatory_args).add(args.optional_args);
		print_usage(cerr, string(*argv), "--file1 FILE --file2 FILE [OPTIONS]\n"
											" > UNIQUE.RESULTS",
							visible_args, exe_description, 80);
		return false;
	}
	check_required_options(vm, mandatory_args);
	if (vm.count("debug"))
	{
		args.file_1				= "file1_only.ids";
		args.file_2				= "file2_only.ids";
		args.file_1_full		= "file1_only.results";
		args.file_2_full		= "file2_only.results";
		args.file_shared		= "shared.ids";
		args.file_shared_full	= "shared.results";
		args.file_merged		= "merged.ids";
		args.file_merged_full	= "merged.results";

	}


	args.open_err_log();
	return true;
}













